package com.seashell.appstartupjob.config.parser;

import java.util.HashSet;
import java.util.List;

import com.seashell.appstartupjob.config.node.JarFileTransJobNode;
import com.seashell.appstartupjob.worker.IJobWorker;
import com.seashell.appstartupjob.enums.CopyType;
import com.seashell.appstartupjob.enums.DestinationPathType;
import com.seashell.appstartupjob.exceptions.JobConfigNotCorrectException;
import com.seashell.appstartupjob.utils.ElementUtil;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Element;

import com.seashell.appstartupjob.config.IJobConfig;
import com.seashell.appstartupjob.config.JarFileTransJobConfig;

/**
 * com.seashell.appstartupjob.config.parser.JarFileTransJobConfigParser
 * JarFile移动工作配置解析类
 *
 * @author Allen
 * @version v1.0
 * @date 2017/7/14
 */
public class JarFileTransJobConfigParser implements IJobConfigParser {

    private static CopyType DEFAULT_COPY_TYPE = CopyType.OVERRIDE;

    @Override
    public IJobConfig parse(Element node) {
        if(node == null){
            throw new JobConfigNotCorrectException("job config is null");
        }
        JarFileTransJobConfig config = new JarFileTransJobConfig();

        String copyType = ElementUtil.getElementValue(node, JarFileTransJobNode.COPY_TYPE);
        String desPath = ElementUtil.getElementValue(node,JarFileTransJobNode.DESTINATION_PATH);
        String desPathType = ElementUtil.getElementValue(node,JarFileTransJobNode.DESTINATION_PATH_TYPE);
        String expression = ElementUtil.getElementValue(node,JarFileTransJobNode.EXPRESSION);
        String resourcePathCut = ElementUtil.getElementValue(node,JarFileTransJobNode.RESOURCE_PATH_CUT);
        String workerClass = ElementUtil.getElementValue(node,JarFileTransJobNode.WORKER_CLASS);

        if(StringUtils.isEmpty(expression) ||
                StringUtils.isEmpty(desPath) ||
                StringUtils.isEmpty(desPathType)){
            throw new JobConfigNotCorrectException("expression,desPath or desPathType is null");
        }

        if(StringUtils.isNotEmpty(workerClass)){
            try {
                Class<? extends IJobWorker> workerClassType = (Class<? extends IJobWorker>)Class.forName(workerClass);
                config.setWorkerClass(workerClassType);
            } catch (ClassNotFoundException e) {
                throw new JobConfigNotCorrectException("工人类型:"+workerClass+" 不是IJobWorker的实现类");
            }
        }

        config.setExpression(expression.trim());
        config.setDestinationPathType(DestinationPathType.parse(desPathType.trim()));
        config.setDestinationPath(desPath.trim());
        config.setCopyType(copyType==null?DEFAULT_COPY_TYPE:CopyType.parse(copyType.trim()));
        config.setResourcePathCut(resourcePathCut==null?null:resourcePathCut.trim());

        List<String> includes = ElementUtil.getListElementValue(node, JarFileTransJobNode.INCLUDES, JarFileTransJobNode.INCLUDE);
        List<String> excludes = ElementUtil.getListElementValue(node, JarFileTransJobNode.EXCLUDES, JarFileTransJobNode.EXCLUDE);

        if(includes!=null && includes.size()!=0){
            HashSet<String> includesSet = new HashSet<>();
            includesSet.addAll(includes);
            config.setIncludes(includesSet);
        }

        if(excludes!=null && excludes.size()!=0){
            HashSet<String> excludesSet = new HashSet<>();
            excludesSet.addAll(excludes);
            config.setExcludes(excludesSet);
        }

        return config;
    }
}
